var ysb={};

layui.use('table', function(){
  var table = layui.table;
  var $ = layui.jquery;
  var token=localStorage.getToken;
  ysb.init=function(){
	  layer.load(1);
	  var name="",value="",groupId="";
	  try{
		  name=$("#name").val();
		  value=$("#value").val();
		  groupId=$("#groupId").val();
	  }catch(e){}
	  table.render({
	    elem: '#dataList',
	    toolbar: '#toolbarOrg',
	    url:basePath+'/sys/dict/list',
	    where:{"token":token,"name":name,"value":value,"groupId":groupId},
	    cols: [[
	       {type: 'checkbox', fixed: 'left'}
	      ,{field:'name', width:200, title: '字典名称'}
	      ,{field:'value', width:200, title: '字典值'}
	      ,{field:'groupName', width:150, title: '所属分组'}
	      ,{field:'code', width:150, title: '字典编码'}
	      ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
	    ]],
	    done: function () {
	        layer.closeAll('loading');
	    }
	    ,page: true
	  });
};
  

//删除数据
ysb.delDict=function(ids){
	layer.load(1);
    $.ajax({
    	type:"POST",
    	url:basePath+"/sys/dict/delete",
    	data:{"token":token,"ids":ids},
        success:function(data){
        	var code=data.code;
        	var msg=data.msg;
        	if(code=="0"||msg=="success"){
        		 layer.msg("数据操作成功");
        		 ysb.init();
        	}else{
        		   layer.msg(data.msg);
        	}
        	layer.closeAll('loading');
        }
    });
    return false;
};


//头工具栏事件
table.on('toolbar(dataList)', function(obj){
  var checkStatus = table.checkStatus(obj.config.id);
  switch(obj.event){
    case 'delList':
      var data = checkStatus.data;
      var ids="";
      if(data!=null&&data.length>0){
    	  for(var i=0;i<data.length;i++){
    		  if(ids==""){ids=data[i].id;}
    		  else{ids+=","+data[i].id;}
    	  }
    	  layer.confirm('您真的要删除选中的数据吗？', function(index){
    		  ysb.delDict(ids);
    	  });
      }else{
    	  layer.msg("请您至少选择一条数据后再提交"); 
      }
    break;
    case 'groupManage':
      var data = checkStatus.data;
      layer.msg('选中了：'+ data.length + ' 个');
    break;
  };
});


  
  //监听行工具事件
  table.on('tool(dataList)', function(obj){
    var data = obj.data;
    if(obj.event === 'del'){
      layer.confirm('您真的要删除这条数据吗？', function(index){
    	 ysb.delDict(data.id)
         layer.close(index);
      });
    } else if(obj.event === 'edit'){
    	localStorage.getId=data.id;
    }
});
  //初始化数据
  ysb.init();
  //用户点击搜索
  $('#queryBut').click(function () {
	 // ysb.init();
  });
});

